//
//  LoginViewController.m
//  Dear
//
//  Created by 陈俊俊 on 15/7/11.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "LoginViewController.h"
#import "TabBarController.h"
#import "RegisterViewController.h"
#import "AppDelegate.h"
#import "UploadRequest.h"
#import "ForgetPassViewController.h"


#define TopHeight 100
#define LoginViewHeight 200
#define LeftWidth 30
#define LeftMargin 18
#define TextFiledHeight 44

//按钮的tag
typedef enum {
    LoginTag = 1001,
    RegisterTag ,
    ForgetTag
} Tag;


@interface LoginViewController ()<UITextFieldDelegate>

//登陆视图的View
@property (nonatomic,strong)UIView          *loginView;
@property (nonatomic,strong)UITextField     *userTextField;
@property (nonatomic,strong)UITextField     *passTextField;
@property (nonatomic,strong)UIButton        *registerBtn;
@property (nonatomic,strong)UIButton        *forgetBtn;
@property (nonatomic,strong)UIButton        *loginBtn;

@end

@implementation LoginViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    //初始化视图
    [self createLoginView];
}
#pragma mark - 视图将要显示的时候
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
#pragma mark - 初始化视图
- (void)createLoginView
{
    CGFloat originY = (ScreenSize.height - LoginViewHeight) /2;
       
    self.loginView = [[UIView alloc]initWithFrame:CGRectMake(0, originY, ScreenSize.width, LoginViewHeight)];
    self.loginView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.view addSubview:self.loginView];
   
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, TextFiledHeight*2)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.loginView addSubview:topView];
    
    UIImageView *leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(LeftMargin, 11, 15, 20)];
    leftImageView.image = [UIImage imageNamed:@"login1"];
    leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [topView addSubview:leftImageView];
    
    //密码
    UIImageView *passImageView = [[UIImageView alloc]initWithFrame:CGRectMake(leftImageView.frame.origin.x,TextFiledHeight+11, 15, 20)];
    passImageView.image = [UIImage imageNamed:@"login4"];
     passImageView.contentMode = UIViewContentModeScaleAspectFit;
    [topView addSubview:passImageView];
    
    //用户名
    self.userTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(leftImageView.frame) +LeftMargin, 0, ScreenSize.width - CGRectGetMaxX(leftImageView.frame) - LeftMargin*2,TextFiledHeight)];
    self.userTextField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
    self.userTextField.textAlignment = NSTextAlignmentLeft;
    self.userTextField.backgroundColor = [UIColor clearColor];
    self.userTextField.placeholder = @"请输入您的手机号";
    self.userTextField.font = FontSize(15);
    self.userTextField.delegate = self;
    self.userTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [topView addSubview:self.userTextField];
    
    UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin, TextFiledHeight - 1, ScreenSize.width - LeftMargin*2, 1)];
    line.backgroundColor = HexColor(@"e4e4e4");
    [topView addSubview:line];
    //密码
    self.passTextField  = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(passImageView.frame) + LeftMargin, TextFiledHeight, ScreenSize.width - CGRectGetMaxX(passImageView.frame) - LeftMargin*2,TextFiledHeight)];
    self.passTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.passTextField.textAlignment = NSTextAlignmentLeft;
    self.passTextField.backgroundColor = [UIColor clearColor];
    self.passTextField.placeholder = @"请输入您的密码";
    self.passTextField.clearButtonMode =UITextFieldViewModeWhileEditing;
    self.passTextField.font = FontSize(15);
    self.passTextField.delegate = self;
    self.passTextField.secureTextEntry = YES;
    [topView addSubview:self.passTextField];
    
    
    //登陆
    self.loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginBtn.frame       = CGRectMake(LeftMargin,CGRectGetMaxY(topView.frame) + 23,ScreenSize.width - LeftMargin *2,TextFiledHeight);
    [self.loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    self.loginBtn.backgroundColor =  SameColor;
    self.loginBtn.layer.cornerRadius = 5;
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.tag = LoginTag;
    [self.loginBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.loginBtn.titleLabel.font = FontSize(16);
    [self.loginView addSubview:self.loginBtn];
    
    //注册
    self.registerBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registerBtn.frame       = CGRectMake(LeftMargin,CGRectGetMaxY(self.loginBtn.frame) + 5,LeftWidth,TextFiledHeight);
    [self.registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.registerBtn setTitleColor:HexColor(@"00c45c") forState:UIControlStateNormal];
    self.registerBtn.tag = RegisterTag;
    [self.registerBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.registerBtn.titleLabel.font = FontSize(15);
    [self.loginView addSubview:self.registerBtn];
    
    //忘记密码
    self.forgetBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    self.forgetBtn.frame       = CGRectMake(ScreenSize.width - (LeftWidth+30) - LeftMargin,CGRectGetMaxY(self.loginBtn.frame) + 5,(LeftWidth+30),TextFiledHeight);
    [self.forgetBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.forgetBtn setTitleColor:HexColor(@"00c45c") forState:UIControlStateNormal];
    self.forgetBtn.tag = ForgetTag;
    [self.forgetBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.forgetBtn.titleLabel.font = FontSize(15);
    [self.loginView addSubview:self.forgetBtn];
    
//
    _userTextField.text = @"15838350915";
    _passTextField.text = @"123456";
    
}

#pragma mark  - 按钮事件
- (void)btnClick:(UIButton *)btn
{
    if (btn.tag == LoginTag)
    {
      
        //登陆
        NSString *userName = [_userTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSString *password = [_passTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        if (userName.length == 0 || password.length == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"用户名密码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            return;
 
        }
        NSDictionary *dict = @{@"userName":userName,@"passWord":password,@"imei":UUIDStr};
        NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpUserLogin];
        [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            [DearLoadingView hideHUD];
            if (responseObject) {
                NSLog(@"--------%@",UUIDStr);
                NSDictionary *dict = (NSDictionary *)responseObject;
                if ([dict[@"Result"] integerValue] == 0) {
                    if ([dict[@"flag"] integerValue] == 1 && ![UUIDStr isEqualToString:dict[@"userId"]]) {
                        DearUser * dearUser  = [DearUser sharedInstance];
                        [dearUser setValuesForKeysWithDictionary:dict];
                        dearUser.imei = UUIDStr;
                        dearUser.passWord = password;
                        
                        //开启上传地图
                        [UploadRequest sharedInstance];
                        
                        //登陆切换界面
                        TabBarController * tabBar=[[TabBarController alloc]init];
                        [DearAppDelegate window].rootViewController=tabBar;

                    }
                }else {
                    [DearLoadingView showTips:dict[@"Message"]];
                }
            }else{
                [DearLoadingView showTips:@"获取失败"];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [DearLoadingView hideHUD];
            [DearLoadingView showTips:error];
        }];

    }else if (btn.tag == RegisterTag)
    {
        //注册
        RegisterViewController *rvc = [RegisterViewController new];
        [self.navigationController pushViewController:rvc animated:YES];
        
        
    }else if (btn.tag == ForgetTag)
    {
        ForgetPassViewController *fvc = [ForgetPassViewController new];
        [self.navigationController pushViewController:fvc animated:YES];
    }
}

//收键盘操作
- (void)keyboardHidden{
    [self.userTextField resignFirstResponder];
    [self.passTextField resignFirstResponder];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
@end
